package com.example.springbootkafka.producer;


import com.example.springbootkafka.configration.KafkaFutureCallback;
import com.example.springbootkafka.constant.KafkaConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class KafkaProducer3 {
    private static final Logger log = LoggerFactory.getLogger(KafkaProducer3.class);
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;
    @Autowired
    private KafkaFutureCallback kafkaFutureCallback;

    /**
     * Constructor >> @Autowired >> @PostConstruct
     * 如果想在生成对象时完成某些初始化操作，而偏偏这些初始化操作又依赖于依赖注入，那么久无法在构造函数中实现。
     * 为此，可以使用@PostConstruct注解一个方法来完成初始化，@PostConstruct注解的方法将会在依赖注入完成后被自动调用。
     *
     * 经过测试
     * InitializingBean > @PostConstruct >  CommandLineRunner = ApplicationRunner
     */
    @PostConstruct
    public void init(){
        log.info("这里是@PostConstruct");
        kafkaTemplate.send(KafkaConstant.TOPIC1.getValue(),"这里是@PostConstruct").addCallback(kafkaFutureCallback);
    }


}
